/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package argtrust.cmd;

import java.lang.System;
import java.util.Collection;
import java.util.List;
import aima.core.logic.fol.parsing.FOLParser;
import aima.core.logic.fol.parsing.ast.Sentence;
import aima.core.logic.fol.domain.FOLDomain;
import aima.core.logic.fol.Connectors;

import argtrust.logic.FOLNaturalDeduction;
import argtrust.logic.FOLKnowledgeBase;
import argtrust.logic.InferenceRule;
import argtrust.logic.InferenceNode;
import argtrust.argsys.ArgumentationSystem;
import argtrust.argsys.ArguingNode;
import argtrust.belief.BeliefBase;
import argtrust.trustnet.Agent;
import argtrust.trustnet.TrustNet;
import argtrust.trustnet.TrustNode;
import argtrust.tidal.TidalTrust;
import argtrust.tidal.TidalTrustBelief;
import argtrust.trustnet.ITrustSemantics;
import argtrust.belief.ITrustBeliefSemantics;
import argtrust.argsys.IBeliefArgumentSemantics;
import argtrust.argsys.MinBeliefArgument;
import argtrust.argsys.IArgumentationSemantics;
import argtrust.argsys.LabelGroundedSemantics;


import argtrust.argtrustgraph.ArgTrustGraph;
import argtrust.xml.XMLTrustBeliefReader;

/**
 *
 * @author Yuqing Tang
 */
public class ArgTrustHtml {

    FOLDomain mDomain;
    FOLParser mFol;
    TrustNet mTrustNet;
    BeliefBase mBb;

//    public static void main(String[] args) {
    public void loadXML(String agent_name,
            String query,
            String input_xml,
            String output_uri_location) {
        XMLTrustBeliefReader reader = new XMLTrustBeliefReader();
        reader.readFile(input_xml);
        TrustNet trust_net = reader.getTrustnet();

        Agent agent = null;
        if (agent_name == null) {
            agent = reader.getQueryAgent();
        } else {
            agent = trust_net.getAgent(agent_name);
        }
        if (agent == null) {
            System.out.println("No query agent specifed.");
        }
        //Argumentation system:
        BeliefBase bb = reader.getBeliefBase();
        FOLKnowledgeBase kb = new FOLKnowledgeBase();
        kb.acceptKnowledgeBase(bb.getFactSet(), bb.getRuleSet());
        ArgumentationSystem arg_sys = new ArgumentationSystem(kb);
        List<ArguingNode> root_arg_list = null;
        Sentence query_sentence = null;

        if (query == null) {
            root_arg_list = arg_sys.getArguingNodeList(reader.getQuestionLst());
        } else {
            query_sentence = reader.getFolParser().parse(query);
            System.out.println("Query is :" + query_sentence.toString());
            root_arg_list = arg_sys.getArguingNodeList(query_sentence);

        }


//        ArguingNode root_arg = arg_sys.getArguingNode(query_sentence);

//        root_arg.debug_print(System.out);
        if (root_arg_list == null) {
            System.out.println("No argument can be produced for " + query_sentence.toString());
            return;
        }

        ArgTrustGraph argtrust_graph = new ArgTrustGraph();
        argtrust_graph.setArgSys(arg_sys);
        argtrust_graph.setBeliefBase(reader.getBeliefBase());
        argtrust_graph.setTrustNet(trust_net);
        TidalTrust trust_sem = new TidalTrust(trust_net, 0);
        argtrust_graph.setTrustSemantics(trust_sem);

        ITrustBeliefSemantics trust_belief_sem = new TidalTrustBelief(bb, trust_sem);
        argtrust_graph.setTrustBeliefSemantics(trust_belief_sem);

        IBeliefArgumentSemantics arg_belief_sem = new MinBeliefArgument(trust_belief_sem);
        argtrust_graph.setArgumentBeliefSemantics(arg_belief_sem);

        IArgumentationSemantics argumentation_sem = new LabelGroundedSemantics(arg_sys, arg_belief_sem);
        argtrust_graph.setArgumentationSemantics(argumentation_sem);
//
        argtrust.out.html.core.HtmlArgTrustDrawing drawing_out =
                new argtrust.out.html.core.HtmlArgTrustDrawing(argtrust_graph);
        argtrust.out.html.core.HtmlAttributeFactory drawing_attribute_factory =
                new argtrust.out.html.core.HtmlAttributeFactory(argtrust_graph);
        argtrust_graph.setDrawingOutput(
                drawing_out,
                drawing_attribute_factory);

        argtrust_graph.draw(root_arg_list, trust_net.getTrustNode(agent));

        java.net.URI path_uri = null;
        try {
            path_uri = new java.net.URI(output_uri_location);
        } catch (java.lang.Exception e) {
            System.out.println("Execution in file locations: " + e.getMessage());
        }
        String path = new java.io.File(path_uri).getPath();
        System.out.println(path);
        drawing_out.getHtmlAdaptor().setOutputPath(
                path);
        drawing_out.getHtmlAdaptor().setRootAgentId(drawing_out.getAgentID(agent));
        drawing_out.getHtmlAdaptor().process();
//        argtrust_graph.draw(arg_sys.getAllNodes(), trust_net.getTrustNodes());
        System.out.println(argtrust_graph.getAttributesFactory().getName() + " is used.");

        System.out.println("Argumentation for " + query.toString());
//        root_arg.debug_print(System.out);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        if (args.length < 2) {
            System.out.println("usage: <Argrust cmd> <input xml> <output folder> [agent] [query]");
            return;
        }
        String agent = "john";
        String query = "Watch(hce)";
        String infile = "XML-samples/jcl.xml";
        String output_location = "file:///tmp/argtrust";
        if (args.length > 0) {
            infile = args[0];
        }
        if (args.length > 1) {
            output_location = args[1];
        }
        if (args.length > 2) {
            agent = args[2];
        }
        if (args.length > 3) {
            query = args[3];
        }

        ArgTrustHtml arg_trust = new ArgTrustHtml();
        arg_trust.loadXML(agent, query, infile, output_location);
        System.out.println("HTML files are located at: " + output_location + ".");
    }
}
